package com.software.avatar.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.software.avatar.R;
import com.software.avatar.entity.UserInfo;
import com.software.avatar.result.Result;
import com.software.avatar.utils.HostUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class RegisterActivity extends AppCompatActivity {

    private Button btn_register;
    private EditText edt_register_phone;
    private EditText edt_register_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
        initEvents();
    }

    private void initEvents() {
        //注册事件
        btn_register.setOnClickListener(v -> {
            String phone = edt_register_phone.getText().toString();
            String password = edt_register_password.getText().toString();

            //发送请求到servlet
            new Thread(){
                @Override
                public void run() {
                    register(phone, password);
                }
            };
        });
    }

    private void register(String phone, String password) {
        InputStream is = null;
        try {
            URL url = new URL(HostUtil.HOST + "/register?phone=" + phone + "&password=" + password);
            is = url.openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String json = br.readLine();
            Gson gson = new Gson();
            Result result = gson.fromJson(json, new TypeToken<Result<UserInfo>>() {
            }.getType());
            if (result.getCode() != 200) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RegisterActivity.this, result.getMsg(), Toast.LENGTH_LONG).show();
                    }
                });
            } else {
                //gotoLogin
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                        gotoLogin();
                    }
                });

            }
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    private void gotoLogin() {
        Intent intent = new Intent(
                RegisterActivity.this,
                LoginActivity.class
        );
        startActivity(intent);
    }

    private void initViews() {
        btn_register = findViewById(R.id.btn_register);
        edt_register_phone = findViewById(R.id.edt_register_phone);
        edt_register_password = findViewById(R.id.edt_register_password);
    }
}